<?php
namespace app\cash\controller;
use think\Controller;
use think\Db;
use app\cash\model\OrderDetails;
use app\cash\model\StoreGoodsLog;
class Recharge extends Controller{
    /**
     * @Author  lingyun
     * @Desc    会员充值
     * 2018-06-10
     */
    public function index(){
        $post = input('post.');
        $check_result = $this->check_form($post);

        $store = db('store')->where('id',session('cash_user')['store_id'])->find();

        if($check_result['code'] == 0){
            $this->error($check_result['msg']);
        }
        $member = $check_result['member'];
        $order = Db::name('user_wallet_log')->where('order_sn',$check_result['order_sn'])->find();
        if($post['pay_type'] == 4 || $post['pay_type'] == 5 || $store['is_jiameng'] == 1 || $post['pay_type'] == 6){
            //查询订单信息
//            $order = Db::name('user_wallet_log')->where('order_sn',$check_result['order_sn'])->find();
            //会员信息
//            $member = Db::name('user')->where('uid',$order['uid'])->find();
            $cur_amount = $member['user_money']+$post['price'];        //会员当前余额

            //变更会员余额
            Db::name('user')->where('uid',$order['uid'])->setInc('user_money',$post['price']);
            //更新订单状态
            Db::name('user_wallet_log')->where('order_sn',$check_result['order_sn'])->update(['pay_time'=>time(),'pay_status'=>1,'pay_type'=>$post['pay_type'],'amount'=>$post['price'],'cur_amount'=>$cur_amount,'sign'=>2,'chargetype'=>2,'charge_cn'=>'余额充值']);
            //记录支付日志
            Db::name('pay_log')->insert(['order_id'=>$order['id'],'order_sn'=>$check_result['order_sn'],'order_type'=>2,'uid'=>$member['uid'],'pay_type'=>$post['pay_type'],'pay_sign'=>2,'amount'=>$post['price'],'addtime'=>time(),'remark'=>'会员充值，订单号'.$order['order_sn'].'，充值金额：'.$order['amount'],'store_id'=>session('cash_user')['store_id']]);


            if($post['pay_type'] == 1){
                $pty = '支付宝';
            }else if($post['pay_type'] == 2){
                $pty = '微信';
            }else if($post['pay_type'] == 4){
                $pty = '现金';
            }else if($post['pay_type'] == 5){
                $pty = '银行卡';
            }else if($post['pay_type'] == 6){
                $pty = '现金卡';
            }

            $receipt = '';          //线下小票信息
            $receipt .= '<tr><td colspan="4" style="font-size: large;text-align: center">'.$store["title"].'</td></tr><tr><td colspan="4" style="font-size: large;text-align: center">&nbsp;</td></tr><tr><td colspan="4" style="text-align: left;font-size: smaller">单号：'.$check_result["order_sn"].'</td></tr><tr><td colspan="4" style="text-align: left;font-size: smaller">收银员：' . session("cash_user")["realname"] . '</td></tr><tr><td colspan="4" style="text-align: center">—————————————————</td></tr>';

            $receipt .='<tr><td colspan="4" style="text-align: left;font-size: smaller">充值金额：'.$post["price"].'</td></tr><tr><td colspan="4" style=";text-align: center">&nbsp;</td></tr>';

            $receipt .='<tr><td colspan="4" style="text-align: left;font-size: smaller">姓名：' . $member["realname"] . '     手机：' . $member["mobile"] . '</td></tr><tr><td colspan="4" style=";text-align: center">&nbsp;</td></tr>';
            $receipt .='<tr><td colspan="4" style="text-align: left;font-size: smaller">会员卡：' . $member["pay_code"] . '     余额：￥' . $cur_amount . '</td></tr><tr><td colspan="4" style=";text-align: center">&nbsp;</td></tr>';

            $receipt .='<tr><td colspan="4" style="text-align: left;font-size: smaller">付款方式：' . $pty . '</td></tr>';

            $receipt .= '<tr><td colspan="4" style="text-align: left;font-size: smaller;">' . $store["address"] . '</td></tr>';
            $receipt .= '<tr><td colspan="4" style="text-align: left">' . date('Y-m-d H:i:s') . '</td></tr>';
            $receipt .= '<tr><td colspan="4" style="text-align: left">' . '欢迎您再次光临' . '</td></tr>';

            //门店会员充值金额
            $store_credit_log = [
                'recharge_amount'=>$store['recharge_amount']+$post['price']
            ];
            db('store')->where('id',$store['id'])->update($store_credit_log);       //记录门店充值记录

            //会员充值三级分销变更记录
            recharge_third_distribution($member['uid'],$post['price']);
            $this->success('充值成功','',$receipt);
        }else if($post['pay_type'] == 1){           //支付宝支付
            $pay_result = $this->redirect(url('Alipay/recharge').'?order_id='.$order['id']."&auth_code=".$post['pay_code']);
        }else if($post['pay_type'] == 2){           //微信支付
            $pay_result = $this->redirect(url('Wxpay/recharge').'?order_id='.$order['id']."&auth_code=".$post['pay_code']);
        }

    }

    /**
     * @Author  lingyun
     * @Desc    检查充值订单
     * 2018-06-10
     */
    private function check_form($param){
        $reg = '/^\d+(\.\d{1,2})?$/';       //匹配金额
        $amount = 0;        //充值金额

        if(empty($param['user_mobile'])){          //请输入会员手机
            return ['code'=>0,'msg'=>'请输入会员手机号或会员卡号'];
        }

        if(empty($param['price'])){
            return ['code'=>0,'msg'=>'请输入充值金额','err_url'=>url('User/recharge'),'err_button'=>'充值'];
        }else if(!preg_match($reg,$param['price'])){
            return ['code'=>0,'msg'=>'充值金额输入错误，请重新填写','err_url'=>url('User/recharge'),'err_button'=>'充值'];
        }
        $amount = $param['price'];

        if($param['pay_type'] == ''){
            return ['code'=>0,'msg'=>'请选择支付方式','err_url'=>url('User/recharge'),'err_button'=>'充值'];
        }
        $member_result = $this->get_member($param['user_mobile']);
        if($member_result['code'] == 0){
            return ['code'=>0,'msg'=>$member_result['msg'],'err_url'=>url('User/recharge'),'err_button'=>'充值'];
        }
        $member = $member_result['msg'];

        //订单编号
        $order_sn = create_order_sn($member['uid']);
        //会员充值变更记录
        $data = [
            'uid'=>$member['uid'],
            'store_id'=>session('cash_user')['store_id'],
            'order_id'=>0,
            'order_sn'=>$order_sn,
            'pay_status'=>0,
            'pay_type'=>$param['pay_type'],
            'amount'=>$amount,
            'give_away_amount'=>0,
            'cur_amount'=>$member['user_money']+$amount,
            'sign'=>2,
            'chargetype'=>2,
            'charge_cn'=>'余额充值',
            'addtime'=>time()
        ];
        $res = db('user_wallet_log')->insertGetId($data);
        return ['code'=>1,'order_sn'=>$order_sn,'pay_type'=>$param['pay_type'],'member'=>$member];
    }

    /**
     * @Author  lingyun
     * @Desc    查询会员信息
     */
    public function get_member($key){
        $key = input('user_mobile');         //会员信息

        if($key == ''){
            return ['code'=>0,'msg'=>'请输入会员手机号或会员卡号'];
        }

        $member = db('user')->where('mobile',$key)->find();
        if(empty($member)){
            $member = db('user')->where('pay_code',$key)->find();
        }

        if(empty($member)){
            return ['code'=>0,'msg'=>'找不到会员信息，请检查输入信息是否正确'];
        }else{
            return ['code'=>1,'msg'=>$member];
        }
    }


}